package com.ht.api.gaoxian;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@SpringBootTest
public class InitRuleConfig {


    @Test
    public void init () {
        List<Entity> dataList = new ArrayList<>();

        DbList tableList = Db.query("select * from rule_mapping where sys_deleted = 1");

        for (Entity entity : tableList) {
            String tableCode = entity.getStr("table_code");
            DbList columnList = Db.query("select * from db_column where table_code = ?", tableCode);

            for (Entity entries : columnList) {
                String column = entries.getStr("code");
                if (column.equals("sys_time") || column.equals("sys_deleted") || column.equals("sys_creator") || column.equals("oneid")) {
                    continue;
                }

                Entity ruleKey = Entity.create("rule_key");
                ruleKey.set("column_id", entries.getMainId());
                ruleKey.set("name", entries.getStr("name"));
                ruleKey.set("is_relation", 0);
                ruleKey.set("mapping_id", entity.getMainId());


                boolean isAdd = true;
                String type = entries.getStr("type");
                if (!entries.isNull("dict_id")) {
                    ruleKey.set("value_type", "dict");
                } else if (type.equals("varchar")) {
                    ruleKey.set("value_type", "string");
                } else if (type.equals("datetime")) {
                    ruleKey.set("value_type", "datetime");
                } else if (type.equals("int")) {
                    ruleKey.set("value_type", "int");
                } else if (type.equals("decimal")) {
                    ruleKey.set("value_type", "int");
                } else {
                    isAdd = false;
                }

                if (isAdd) {
                    dataList.add(ruleKey);
                }
            }


        }

        Db.insertBatchWithStream(dataList);

        try {
            log.info(".....");
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


//    @Test
//    public void initaaa () {
//
//        List<Entity> dataList = new ArrayList<>();
//
//        DbList tableList = Db.query("select * from db_table where group_id in (select main_id from sys_group where parent_id = 1877906840768090112)");
//        for (Entity entity : tableList) {
//            String tableCode = entity.getStr("code");
//            String tableName = entity.getStr("name");
//
//
//            DbList columnList = Db.query("select * from db_column where table_code = ?", tableCode);
//
//
//            Entity rule_mapping = Entity.create("rule_mapping");
//            for (Entity entries : columnList) {
//
//
//
//            }
//
//
//        }
//
//        Db.insertBatchWithStream(dataList);
//
//        try {
//            log.info(".....");
//            System.in.read();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }



}
